package com.lanren.familybookapp.ui.me.model;

import com.lanren.common.app.ApiUrl;
import com.lanren.common.base.impl.BaseModel;
import com.lanren.common.http.HttpManager;
import com.lanren.easyhttp.callback.SimpleCallBack;
import com.lanren.easyhttp.exception.ApiException;
import com.lanren.familybookapp.app.AppUserHelper;
import com.lanren.familybookapp.app.UserInfoBean;
import com.lanren.familybookapp.ui.me.bean.BookInfoBean;
import com.lanren.familybookapp.ui.me.interfaces.IManagerModel;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.disposables.Disposable;

/**
 * @ClassName BookManagerModel
 * @Description TODO
 * @Author chongheng.wang
 * @Date 2020/2/12 15:21
 * @Version 1.0
 */
public class BookManagerModel extends BaseModel {
    public BookManagerModel() {
        initDispoable();
    }

    //获取账本列表
    public void getBookList(int page, final IManagerModel.ManagerModel managerModel) {

        Map<String, String> map = new LinkedHashMap<>();
        map.put("page", page + "");
        Disposable disposable = HttpManager.post(ApiUrl.API.BOOK_MANAGER_LIST)
                .headers(AppUserHelper.getHttpHeaders())
                .params(map)
                .execute(new SimpleCallBack<List<BookInfoBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        managerModel.onError(e.getCode(), e.getMessage());
                    }

                    @Override
                    public void onSuccess(List<BookInfoBean> bookInfoBeans) {
                        managerModel.onSuccess(bookInfoBeans);

                    }
                });
        addDisposable(disposable);

    }

    public void addBook(String bookName, final IManagerModel.ManagerBookAddModel managerModel) {

        Map<String, String> map = new LinkedHashMap<>();
        map.put("bookName", bookName);
        Disposable disposable = HttpManager.post(ApiUrl.API.BOOK_MANAGER_ADD)
                .headers(AppUserHelper.getHttpHeaders())
                .params(map)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 10005) {//已存在该账本
                            managerModel.onSuccess(e.getMessage());
                        } else if (e.getCode() == 200) {
                            managerModel.onSuccess(e.getMessage());
                        } else {
                            managerModel.onError(e.getCode(), e.getMessage());
                        }
                    }

                    @Override
                    public void onSuccess(String s) {
                        managerModel.onSuccess(s);
                    }

                });
        addDisposable(disposable);
    }

    public void delBook(int bookId, final IManagerModel.ManagerBookAddModel managerModel) {

        Map<String, String> map = new LinkedHashMap<>();
        map.put("bookId", bookId + "");
        Disposable disposable = HttpManager.post(ApiUrl.API.BOOK_MANAGER_DEL)
                .headers(AppUserHelper.getHttpHeaders())
                .params(map)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 200) {
                            managerModel.onSuccess(e.getMessage());
                        } else {
                            managerModel.onError(e.getCode(), e.getMessage());
                        }
                    }

                    @Override
                    public void onSuccess(String s) {
                        managerModel.onSuccess(s);
                    }

                });
        addDisposable(disposable);

    }


    public void updateUseBook(int userUseBook, final IManagerModel.ManagerBookAddModel managerModel) {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("userUseBook", userUseBook + "");
        HttpManager.post(ApiUrl.API.USER_INFO_UPDATE)
                .headers(AppUserHelper.getHttpHeaders())
                .params(map)
                .execute(new SimpleCallBack<UserInfoBean>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 200) {
                            managerModel.onSuccess(e.getMessage());
                        } else {
                            managerModel.onError(e.getCode(), e.getMessage());
                        }
                    }

                    @Override
                    public void onSuccess(UserInfoBean userInfoBean) {
                        AppUserHelper.getInstance().setUserInfoBean(userInfoBean);
                        managerModel.onSuccess("修改默认成功");
                    }

                });
    }
}
